﻿using MediatR;
using Newtonsoft.Json;
using RBAC_Get.API.Infrastructures.Interfaces;
using Return.Enum;
using THE_WIND_RISES.Common.Domans.RBAC;
using THE_WIND_RISES.Common.Dtos;

namespace RBAC_Get.API.Application.Commands
{
    /// <summary>
    /// 登录
    /// </summary>
    public class ShowUserHandler : IRequestHandler<ShowUserCommand, THE_WIND_RISES.Common.Dtos.Result>
    {
        public IBaseRepositoryGet<User> UserRep;
        public IBaseRepositoryGet<UserRole> UserRoleRep;
        public IBaseRepositoryGet<Role> RoleRep;
        public IBaseRepositoryGet<RolePerm> RolePermRep;
        public IBaseRepositoryGet<Permission> PermissionRep;
        ILogger<ShowUserHandler> logger;

        public ShowUserHandler(IBaseRepositoryGet<User> userRep, IBaseRepositoryGet<Role> roleRep, IBaseRepositoryGet<Permission> permissionRep, ILogger<ShowUserHandler> logger, IBaseRepositoryGet<UserRole> userRoleRep, IBaseRepositoryGet<RolePerm> rolePermRep)
        {
            UserRep = userRep;
            RoleRep = roleRep;
            PermissionRep = permissionRep;
            this.logger = logger;
            UserRoleRep = userRoleRep;
            RolePermRep = rolePermRep;
        }

        public async Task<Result> Handle(ShowUserCommand request, CancellationToken cancellationToken)
        {
            var result = new THE_WIND_RISES.Common.Dtos.Result() { Code = (int)CommonErrorCodes.Search };
            try
            {
                Console.WriteLine("开始登录"+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                Console.WriteLine(JsonConvert.SerializeObject(request));
                var UserLoginSql = $"select * from User where UserName = '{request.UserName}' and UserPwd = '{request.UserPwd}'";
                var UserRoleSql = $"select * from UserRole";
                var RoleSql = "select * from Role";
                var RolePermSql = "select * from RolePerm";
                var PermissionSql = "select * from Permission";
                Console.WriteLine(UserLoginSql);
                var LoginData = from a in UserRep.GetAll(UserLoginSql)
                                join b in UserRoleRep.GetAll(UserRoleSql)
                                on a.UserId equals b.UserId
                                join c in RoleRep.GetAll(RoleSql)
                                on b.RoleId equals c.RoleId
                                join d in RolePermRep.GetAll(RolePermSql)
                                on c.RoleId equals d.RoleId
                                join e in PermissionRep.GetAll(PermissionSql)
                                on d.PermissionId equals e.PermissionId
                                where a.IsDelete == false && c.IsDelete == false && e.IsDelete == false
                                select new
                                {
                                    a.UserId,
                                    a.UserName,
                                    c.RoleId,
                                    c.RoleName,
                                    e.PermissionId,
                                    e.PermissionName
                                };
                Console.WriteLine(LoginData.Count());
                if (LoginData.Count() > 0)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    result.Data = LoginData;
                    result.Msg = $"用户{request.UserName}登录成功";
                    logger.LogError($"用户{request.UserName}登录成功");
                }
                else
                {
                    result.Code = (int)CommonErrorCodes.Search;
                    result.Data = null;
                    result.Msg = "登录失败，用户和密码不匹配";
                    logger.LogError($"用户{request.UserName}登录失败，用户和密码不匹配");
                }
            }
            catch (Exception ex)
            {
                logger.LogError($"用户{request.UserName}登录失败，用户和密码不匹配");
                throw ex;
            }
            return result;
        }
    }
}
